package com.ruoyi.common.jpush;

import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @ClassName JPushClientUtil
 * @Description: TODO
 * @Author shihx
 * @Date 2020/2/14
 * @Version V1.0
 **/
public class JPushClientUtil {

    private static Logger LOG = LoggerFactory.getLogger(JPushClientUtil.class);

    /**
     * 增加volatile修改
     */
    private volatile static JPushClientUtil instance = null;

    // 极光官网-个人管理中心-appkey
    private String appkey;

    // 极光官网-个人管理中心-点击查看-secret
    private String secret;

    private JPushClient jPushClient;

    private JPushClientUtil(){

    }

    public static JPushClientUtil getInstance(){
        //先检查实例是否存在，如果不存在才进入下面的同步块
        if(instance == null){
            //同步块，线程安全的创建实例
            synchronized(JPushClientUtil.class){
                //再次检查实例是否存在，如果不存在才真的创建实例
                if(instance == null){
                    instance = new JPushClientUtil();
                }
            }
        }
        return instance;
    }



    // 推送客户端
//    @PostConstruct
    public void initJPushClient(String secret, String appkey) {
        jPushClient = new JPushClient(secret, appkey);
    }


    /**
     * 获取推送客户端
     * @methodName: getJPushClient
     * @description: TODO
     * @param
     * @return: cn.jpush.api.JPushClient
     * @author: shihx
     * @date: 2020/2/17 16:59
    **/
    public JPushClient getJPushClient() {
        return jPushClient;
    }

    /**
     * IOS 单人推送
     * @methodName: sendPush
     * @description: TODO
     * @param registrationId
     * @param title
     * @param body
     * @param jsonObject
     * @return: cn.jpush.api.push.PushResult
     * @author: shihx
     * @date: 2020-04-01 10:33
    **/
    public PushResult sendPush(String registrationId,String title,String body, JsonObject jsonObject) throws APIConnectionException, APIRequestException {
        PushResult result = null;
        Options options=Options.sendno();
        options.setApnsProduction(true);
        PushPayload payload = PushPayload.newBuilder()
                .setAudience(Audience.registrationId(registrationId))
                .setPlatform(Platform.ios())
                .setOptions(options)
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification
                                .newBuilder()
                                .setSound("happy")
                                .setBadge(0)
                                .setContentAvailable(true)
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(title, "", body)
                                        .build()
                                )
                                .addExtra("extra", jsonObject)
                                .build())
                        .build())
                .build();

//        try {
            result = jPushClient.sendPush(payload);
//            LOG.info("Got result - " + result);
//        } catch (APIConnectionException e) {
//            // Connection error, should retry later
//            LOG.error("Connection error, should retry later", e);
//        } catch (APIRequestException e) {
//            // Should review the error, and fix the request
//            LOG.error("Should review the error, and fix the request", e);
//            LOG.info("HTTP Status: " + e.getStatus());
//            LOG.info("Error Code: " + e.getErrorCode());
//            LOG.info("Error Message: " + e.getErrorMessage());
//        }
        return result;
    }

    /**
     * IOS全部推送
     * @methodName: sendPushAll
     * @description: TODO
     * @param title
     * @param body
     * @param jsonObject
     * @return: cn.jpush.api.push.PushResult
     * @author: shihx
     * @date: 2020-04-01 10:34
    **/
    public PushResult sendPushAll(String title,String body, JsonObject jsonObject){
        PushResult result = null;
        PushPayload payload = PushPayload.newBuilder()
                .setAudience(Audience.all())
                .setPlatform(Platform.ios())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification
                                .newBuilder()
                                .setSound("happy")
                                .setBadge(0)
                                .setContentAvailable(true)
                                .setAlert(IosAlert.newBuilder()
                                        .setTitleAndBody(title, "", body)
                                        .build()
                                )
                                .addExtra("extra", jsonObject)
                                .build())
                        .build())
                .build();

        try {
            result = jPushClient.sendPush(payload);
            LOG.info("Got result - " + result);
        } catch (APIConnectionException e) {
            // Connection error, should retry later
            LOG.error("Connection error, should retry later", e);
        } catch (APIRequestException e) {
            // Should review the error, and fix the request
            LOG.error("Should review the error, and fix the request", e);
            LOG.info("HTTP Status: " + e.getStatus());
            LOG.info("Error Code: " + e.getErrorCode());
            LOG.info("Error Message: " + e.getErrorMessage());
        }
        return result;
    }



}
